<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="org.teammach.laddercomp.dao.LadderManager"%>
<%@ page import="org.teammach.laddercomp.shared.Ladder"%>
<%@ page import="org.teammach.laddercomp.shared.Team"%>
<%@ page import="com.google.appengine.api.datastore.Entity"%>

<%@ page import="java.util.List"%>

<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css">
<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-23998388-1']);
 _gaq.push(['_trackPageview']);

 (function() {
   var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
 })();

</script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
          <%
   		LadderManager manager = LadderManager.INSTANCE;

      	Ladder ladder = manager.getLadder(request.getParameter("user"));
      	Ladder official = manager.getLadder("Official");
      	%>
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Round');
        data.addColumn('number', '<%=ladder.getUser()%>');
		
		<%

    	List<Integer> prevs = ladder.getPrevRounds();
        System.out.println(prevs.size());
    	for(int i=0; i<prevs.size();i++) {
    		if(prevs.get(i) != -101) {

	%>
		
		data.addRow(['<%=""+(i+1)%>', <%=prevs.get(i)%>]);
		<%
    		}
    		}%>

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 420, height: 240});
      }
    </script>
</head>
<body>
<div id="wrapper">

<a href="/">
<span id="header">
</span>
</a>
<%
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();

 %>

<div id="page">

<div id="content">
 <%
 	if (ladder == null) {
 %>

<h1>No user of the name "<%=request.getParameter("user")%>" was
found</h1>
<%
	} else if (official == null) {
%> Couldn't get the official ladder! <%
	} else {
		int total = 0;
%>

<h1 class="title"><%=ladder.getUser()%></h1>
<p style="text-align:right;padding-right:10px"><a href="standings.jsp">Return to standings</a></p>

<table width="70%">
	<tr>
		<th width="5%"> </th>
		<th width="50%">Team</th>
		<th width="20%">Current</th>
		<th width="25%">Points</th>
	</tr>
	<%
		boolean topEight = true;

			List<String> teams = ladder.getTeams();
			for (int i = 0; i < teams.size(); i++) {
				int predicted = i + 1;
				int actual = official.getTeams().indexOf(teams.get(i)) + 1;
				int difference = Math.abs(actual - predicted);
				total += (5 - difference);
				if (topEight && i < 8) {
					if (actual > 8)
						topEight = false;
				}
	%>
	<tr <%if (i % 2 == 0) {%> class="alt" <%}%>>
		<td><%=i +1 %></td>
		<td><%=teams.get(i)%></td>
		<td><%="" + actual + LadderManager.getOrdinalFor(actual)%></td>
		<td><%=5 - difference%></td>
	</tr>
	<%
		}
	%>

	<tr class="bonus">
		<td></td>
		<td>Top bonus</td>
		
		<td></td>		
		<%
			if (teams.get(0).equalsIgnoreCase(official.getTeams().get(0))) {
					total += 5;
		%>
			<td><%=5%></td>
		<%
			} else {
		%>
			<td><%=0%></td>
		<%
			}
		%>
	</tr>

	<tr class="bonus">
		<td></td>
		<td>Bottom bonus</td>
		<td></td>		
		<%
			if (teams.get(teams.size()
					-1).equalsIgnoreCase(official.getTeams().get(teams.size() -1))) {
					total += 5;
		%>
			<td><%=5%></td>
		<%
			} else {
		%>
			<td><%=0%></td>
		<%
			}
		%>
	</tr>

	<tr class="bonus">
		<td></td>
		<td>Top 8 bonus</td>
		<td></td>		
		<%
			if (topEight) {
					total += 5;
		%>
			<td><%=5%></td>
		<%
			} else {
		%>
			<td><%=0%></td>
		<%
			}
		%>
	</tr>

	<tr class="total">
<td></td>
		<td>Total Score</td>
		<td></td>

		<td><%=total%></td>
	</tr>

</table>


<p style="text-align:center;padding:10px"><%=ladder.getCommentary()%></p>



<%
	}
%>

<%	if (user != null
		&& (user.getEmail().equalsIgnoreCase("dscerri@gmail.com") || user
				.getEmail().equalsIgnoreCase("haydns33@gmail.com"))) {
	%>

<FORM METHOD=POST ACTION="updateCommentary.jsp" style="padding:10px">
Update commentary <br>
<textarea name="commentary" rows="5" cols="60">
<%=ladder.getCommentary() %>
</textarea>
<input type="hidden" name="username" value="<%=ladder.getUser() %>">
<P><INPUT TYPE=SUBMIT>
</FORM>

<%} %>


</div>


<div>
<div id="sidebar">
<h1 class="title">Current Ladder</h1>
<table width="90%">
	<tr>
		<th>Team</th>
		<th>P</th>
		<th>W</th>
		<th>L</th>
		<th>D</th>
		<th>PTS</th>
	</tr>
	<%
		int i=0;
		for (Team team : manager.getTeams()) {
	%>
	<tr<%if (i++ % 2 == 0) {%> class="alt" <%}%>>
		<td><%=team.getTeamName()%></td>
		<td><%=team.getPlayed()%></td>
		<td><%=team.getWon()%></td>
		<td><%=team.getLost()%></td>
		<td><%=team.getDrawn()%></td>
		<td><%=team.getPoints()%></td>
	</tr>
	<%
		}
	%>


	</table>

	</div>
	
	<div id="sidebar">
<h1 class="linksTitle">Links</h1>



	<div  style="text-align:center;margin-top:-10px;padding:5px">
		<ul>
			<li><a href="standings.jsp">Current Standings</a></li>
			<li><a href="rules.jsp">Rules and regulations</a> </li>
			<li><a href="blog.jsp">Haydn's weekly blog</a> </li>
			<li><a href="mailto:dscerri@gmail.com">Contact Webmaster</a> </li>
				<%
		if (user != null
				&& (user.getEmail().equalsIgnoreCase("dscerri@gmail.com") || user
						.getEmail().equalsIgnoreCase("haydns33@gmail.com"))) {
	%>
	<li><a href="<%=userService.createLogoutURL(request.getRequestURI() + "?" + request.getQueryString())%>">Log out</a>	</li><%
		} else {
	%>
	<li><a href="<%=userService.createLoginURL(request.getRequestURI() + "?" + request.getQueryString())%>">Admin login</a></li>
	<%
		}
	%>
		</ul>
	</div>
	</div>

			
	</div>
<div id="content">
<h1 class="title">History</h1>
<div id="chart_div" style="padding-left:25px"></div></div>
	</div>

</div>


<div id="footer">
<p>&copy;2011 All Rights Reserved &nbsp;&bull;&nbsp; Design by Dave
Scerri Productions &nbsp;&bull;&nbsp; CSS Template from <a
	href="http://www.freecsstemplates.org/">Free CSS Templates</a> .</p>
</div>
</body>
</html>